package random.fox.p2p.ui.customview;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import random.fox.p2p.R;
import random.fox.p2p.bean.Dynamic;
import random.fox.p2p.bean.Fans;
import random.fox.p2p.bean.PersonalityLabel;
import random.fox.p2p.bean.UserInfo;
import random.fox.p2p.ui.DynamicView;
import random.fox.p2p.ui.FansGridView;
import random.fox.p2p.ui.PersonalityView;

/**
 * Created by acer on 2015/3/20.
 */
public class UserInfoAdapter extends BaseAdapter {
    private final String TAG = getClass().getName();
    private int vhSpace =  10;   //GridView 内容间距
    private Context context;

    private UserInfo userInfo;
    private int width , height; //最外层的gridview的宽高

    public UserInfoAdapter( UserInfo userInfo,int width , int height) {
        this.userInfo = userInfo;
        this.width = width;
        this.height = height;
    }


    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public Object getItem(int position) {
        Object obj = null;
        if (userInfo != null) {
            switch (position) {
                case 0:
                    obj = userInfo.getPersonalityLabels();
                    break;
                case 1:
                    obj = userInfo.getDynamics();
                    break;
                case 2:
                    obj = userInfo.getFansList();
                    break;
            }
        }
        return obj;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(context==null)
            context = parent.getContext();
        View view = null;
        switch (position) {
            case 0:
                view = productPersonalityView(userInfo.getPersonalityLabels());
                break;
            case 1:
                view = productDynamicView(userInfo.getDynamics());
                break;
            case 2:
                view = productFansView(userInfo.getFansList());
                break;
        }
        return view;
    }


    private View productPersonalityView(List<PersonalityLabel> personalityLabels) {
       final PersonalityView personalityView = (PersonalityView) View.inflate(context, R.layout.personality_layout, null);
        AbsListView.LayoutParams lp =
                new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT
                        , height);
        personalityView.setLayoutParams(lp);
        personalityView.measure(0, 0);

        GridView gridView =(GridView)personalityView.findViewById(R.id.personality_grid_view);
        View personalityBottomLayout = personalityView.findViewById(R.id.personality_bottom_layout);
        View labelLayout = personalityView.findViewById(R.id.label_tips_layout);

        LinearLayout itemLinearLayout = (LinearLayout) View.inflate(context, R.layout.personality_item, null);
        itemLinearLayout.measure(0,0);

        int bottomLayoutMeasuredHeight = personalityBottomLayout.getMeasuredHeight();
        int labelLayoutMeasuredHeight = labelLayout.getMeasuredHeight();

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.setMargins(0, labelLayoutMeasuredHeight, 0, bottomLayoutMeasuredHeight);
        gridView.setLayoutParams(params);

        int personalityGridViewHeight = height - labelLayoutMeasuredHeight - bottomLayoutMeasuredHeight;
        int personalityItemHeight = itemLinearLayout.getMeasuredHeight();

        int personalityItemWidth = width / (3 * 2);

        gridView.setVerticalSpacing(vhSpace);
        gridView.setHorizontalSpacing(vhSpace);
        gridView.setNumColumns(2);
        gridView.setColumnWidth(personalityItemWidth);

        int rows = personalityGridViewHeight / (personalityItemHeight + vhSpace); //获取行数
        int temp = rows * 2;
        int count = personalityLabels.size() > temp ? temp : personalityLabels.size();

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                personalityView.entry(position);
            }
        });

        PersonalityAdapter adapter = new PersonalityAdapter( personalityLabels , count);
        gridView.setAdapter(adapter);

        return personalityView;
    }

    private View productDynamicView(List<Dynamic> dynamicList) {
        final DynamicView dynamicView = (DynamicView) View.inflate(context, R.layout.dynamic_layout, null);
        TextView textView = (TextView) dynamicView.findViewById(R.id.dynamic_label_title);
        textView.setText("动态("+dynamicList.size()+")");
        View dynamicLabelLayout = dynamicView.findViewById(R.id.dynamic_label_layout);
        AbsListView.LayoutParams lp =
                new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT
                        , height);
        dynamicView.setLayoutParams(lp);
        dynamicView.measure(0, 0);

        GridView gridView = (GridView) dynamicView.findViewById(R.id.dynamic_grid_view);

        int imageItemWidth = width / (3 * 3) - 2*vhSpace;
        gridView.setNumColumns(3);
        gridView.setVerticalSpacing(vhSpace);
        gridView.setHorizontalSpacing(vhSpace);
        gridView.setColumnWidth(imageItemWidth);


        int imageGridViewHeight = height - dynamicLabelLayout.getMeasuredHeight();

        int rows = imageGridViewHeight / (imageItemWidth+vhSpace); //获取行数
        int temp = rows * 3;
        final boolean flag = dynamicList.size() > temp-1 ? true :false;

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (flag && position == parent.getCount()-1){
                    dynamicView.expand();
                }else {
                    dynamicView.entry(position);
                }
            }
        });

        DynamicAdapter adapter = new DynamicAdapter(dynamicList, imageItemWidth, temp , flag);
        gridView.setAdapter(adapter);

        return dynamicView;
    }

    private View productFansView(List<Fans> fansList) {
       final FansGridView fansGridViewLayout = (FansGridView) View.inflate(context, R.layout.fans_layout, null);
        View labelLayout = fansGridViewLayout.findViewById(R.id.fans_label_layout);
        TextView textView = (TextView) fansGridViewLayout.findViewById(R.id.fans_label_title);
        textView.setText("粉丝("+fansList.size()+")");

        AbsListView.LayoutParams lp =
                new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT
                        , height);
        fansGridViewLayout.setLayoutParams(lp);
        fansGridViewLayout.measure(0, 0);
        GridView gridView = (GridView) fansGridViewLayout.findViewById(R.id.fans_grid_view);

        int imageItemWidth = width / (3 * 3 ) - 2*vhSpace;
        gridView.setNumColumns(3);
        gridView.setVerticalSpacing(vhSpace);
        gridView.setHorizontalSpacing(vhSpace);
        gridView.setColumnWidth(imageItemWidth);

        int imageGridViewHeight = height - labelLayout.getMeasuredHeight();

        int rows = imageGridViewHeight / (imageItemWidth + vhSpace ); //获取行数
        int temp = rows * 3;
       final boolean flag = fansList.size() > temp-1 ? true :false;

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (flag && position == parent.getCount()-1){
                    fansGridViewLayout.expand();
                }else {
                    fansGridViewLayout.entry(position);
                }
            }
        });

        FansAdapter adapter = new FansAdapter(fansList, imageItemWidth ,temp , flag );
        gridView.setAdapter(adapter);
        return fansGridViewLayout;
    }
}
